﻿Imports TransportadoraToledoBLL
Public Class frm_cadastro_marcas
#Region "Métodos e Variáveis"
    Public objBLL As MarcasBLL
    Public controle As String
    Dim tab As DataTable
    Public ultimo, primeiro, atual As Integer

    Sub carregaPrimeiroUltimo()
        Try
            objBLL = New MarcasBLL
            ultimo = objBLL.localizarPrimeiroUltimo("ultimo")
            primeiro = objBLL.localizarPrimeiroUltimo("primeiro")
            atual = ultimo
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try

    End Sub


    Sub carregaUltimo()
        Try
            objBLL = New MarcasBLL
            If (ultimo <> 0) Then
                'algum usuario foi clicado
                objBLL.localizar(ultimo)
                If (objBLL.pMar_codigo <> 0) Then
                    carregaCampos()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub
    Sub carregaPrimeiro()
        Try
            objBLL = New MarcasBLL
            If (primeiro <> 0) Then
                'algum usuario foi clicado
                objBLL.localizar(primeiro)
                If (objBLL.pMar_codigo <> 0) Then
                    carregaCampos()

                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Sub limpar()
        txtCodigo.Text = ""
        txtNome.Text = ""
        controle = ""
        'objBLL.limpar()
    End Sub

    Sub status(ByVal sta As Boolean)

        If (txtCodigo.Text = "" Or controle = "A") Then
            bIncluir.Enabled = sta

            bExcluir.Enabled = sta
            If (txtCodigo.Text = "") Then
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
                bExcluir.Enabled = False
            Else
                bExcluir.Enabled = sta
                bPesquisar.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
            End If
            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta
            bPrimeiro.Enabled = sta
            bProximo.Enabled = sta
            bUltimo.Enabled = sta
            bAnterior.Enabled = sta
            gbConteudo.Enabled = Not sta
        Else
            bIncluir.Enabled = sta

            If (txtCodigo.Text = "") Then
                bExcluir.Enabled = False
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
            Else
                bExcluir.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
                bPesquisar.Enabled = sta
            End If

            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta

            gbConteudo.Enabled = Not sta
        End If

        If (atual = primeiro And atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        ElseIf (atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True
        ElseIf (atual = primeiro) Then
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        Else
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True

        End If
    End Sub
    Public Sub carregaCampos()
        Try
            txtCodigo.Text = objBLL.pMar_codigo
            txtNome.Text = objBLL.pMar_nome
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Sub carregaPropriedades()

        Try
            objBLL = New MarcasBLL
            If (txtCodigo.Text = "") Then
                objBLL.pMar_codigo = 0
            Else
                objBLL.pMar_codigo = txtCodigo.Text
            End If
            objBLL.pMar_nome = txtNome.Text
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub associaGrid()
        Try
            tab = New DataTable
            tab.Columns.Add("mod_codigo", GetType(Integer))
            tab.Columns.Add("mar_codigo", GetType(String))
            tab.Columns.Add("mod_nome", GetType(String))
            dgConsulta.DataSource = tab
            dgConsulta.Columns("mod_codigo").HeaderText = "Código"
            dgConsulta.Columns("mod_codigo").Width = 90
            dgConsulta.Columns("mar_codigo").HeaderText = "Marca"
            dgConsulta.Columns("mar_codigo").Width = 280
            dgConsulta.Columns("mod_nome").HeaderText = "Modelo"
            dgConsulta.Columns("mod_nome").Width = 220
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region

    Private Sub bIncluir_Click(sender As System.Object, e As System.EventArgs) Handles bIncluir.Click
        limpar()

        controle = "I"
        status(False)
    End Sub

    Private Sub bAlterar_Click(sender As System.Object, e As System.EventArgs) Handles bAlterar.Click
        controle = "A"

        status(False)
    End Sub

    Private Sub bExcluir_Click(sender As System.Object, e As System.EventArgs) Handles bExcluir.Click
        Try
            Dim objMod As New ModelosBLL

            If (MessageBox.Show("Deseja excluir?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes) Then
                carregaPropriedades()
                objMod.localizaMarcaEx(objBLL.pMar_codigo)
                If objMod.pMar_codigo = 0 Then
                    objBLL.excluir()
                    MessageBox.Show("Excluido com sucesso!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                    limpar()
                    carregaPrimeiroUltimo()
                    carregaUltimo()
                    status(True)
                Else
                    MsgBox("Marca não pode ser excluida, pois está presente em um cadastro de Modelo")
                End If
                
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub

    Private Sub bSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSalvar.Click
        Try
            carregaPropriedades()
            If (controle = "I") Then
                objBLL.inserir()
                MessageBox.Show("Incluido com sucesso!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                limpar()
                carregaPrimeiroUltimo()
                carregaUltimo()
                status(True)
            Else
                objBLL.alterar()
                MessageBox.Show("Alterado com sucesso!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                status(True)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Try
            objBLL = New MarcasBLL
            If (controle = "I") Then
                carregaPrimeiroUltimo()
                carregaUltimo()
            ElseIf (controle = "A") Then
                objBLL.localizar(atual)
                If (objBLL.pMar_codigo <> 0) Then
                    carregaCampos()
                    atual = objBLL.pMar_codigo
                End If
            End If
            controle = ""
            status(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub

    Private Sub bVaPara_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bVaPara.Click
        Try
            Dim frm As New frm_VaPara("seguro")
            frm.ShowDialog()
            If (frm.codigo <> 0) Then
                'algum usuario foi clicado
                objBLL.localizar(frm.codigo)
                If (objBLL.pMar_codigo <> 0) Then
                    carregaCampos()
                    atual = objBLL.pMar_codigo
                Else
                    MessageBox.Show("Registro Inexistente!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    bVaPara_Click(sender, e)
                End If
            End If
            status(True)
            frm = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub

    Private Sub bPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisar.Click
        gbConsulta.Visible = True
        'dgDados.DataSource = Nothing
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y - 127)
        Me.Size = New Size(734, 563)
    End Sub

    Private Sub bPrimeiro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPrimeiro.Click
        carregaPrimeiro()
        atual = primeiro
        status(True)
    End Sub

    Private Sub bAnterior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAnterior.Click
        Try
            objBLL = New MarcasBLL
            Dim codigo = objBLL.localizarProxAnteriro("anterior", atual)
            objBLL.localizar(codigo)
            If (objBLL.pMar_codigo <> 0) Then
                carregaCampos()
                atual = objBLL.pMar_codigo
            End If
            status(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub

    Private Sub bProximo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bProximo.Click
        Try
            objBLL = New MarcasBLL
            Dim codigo = objBLL.localizarProxAnteriro("proximo", atual)
            objBLL.localizar(codigo)
            If (objBLL.pMar_codigo <> 0) Then
                carregaCampos()
                atual = objBLL.pMar_codigo
            End If
            status(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub

    Private Sub bUltimo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bUltimo.Click
        carregaUltimo()
        atual = ultimo
        status(True)
    End Sub

    Private Sub bSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSair.Click
        Me.Close()
    End Sub

    Private Sub bClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bClose.Click
        gbConsulta.Visible = False
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
        Me.Size = New Size(734, 304)
    End Sub

    Private Sub frm_cadastro_marcas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            carregaPrimeiroUltimo()
            carregaUltimo()
            bCancelar_Click(sender, e)
            associaGrid()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bfiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bfiltrar.Click
        Dim tab As DataTable
        Try
            objBLL = New MarcasBLL
            tab = objBLL.localizar(txtConsultar.Text)
            If (tab.Rows.Count > 0) Then
                dgConsulta.DataSource = tab
                dgConsulta.Columns(0).HeaderText = "Código"
                dgConsulta.Columns(1).HeaderText = "Nome da Marca"
                dgConsulta.Columns(0).Width = 100
                dgConsulta.Columns(1).Width = 500
            Else
                MessageBox.Show("Registro(s) não encontrado(s)", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub dgConsulta_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgConsulta.CellDoubleClick
        Dim codigo As Integer
        If (e.RowIndex >= 0) Then
            codigo = dgConsulta.Rows(e.RowIndex).Cells("mar_codigo").Value
            objBLL.localizar(codigo)
            If (objBLL.pMar_codigo <> 0) Then
                carregaCampos()
                atual = objBLL.pMar_codigo
                gbConsulta.Visible = False
                Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
                Me.Size = New Size(734, 304)
            End If
        End If
    End Sub

    Private Sub bAjuda_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAjuda.Click
        Dim Ajuda As String
        Ajuda = Application.StartupPath
        Ajuda = Ajuda.Replace("\bin\Debug", "")
        Ajuda = Ajuda & "\Arquivo de Ajuda\index.html#CadastroMarca"
        Dim Ie As Object
        Ie = CreateObject("InternetExplorer.Application")

        Ie.Visible = True
        Ie.Navigate(Ajuda)
    End Sub
End Class